﻿using EssentialWebFramework.Models.Framework;
using EssentialWebFramework.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using System.Security.Claims;

namespace EssentialWebFramework.Web.Controllers
{
    /// <summary>
    /// 基础控制器，用于实现一些基础的方法
    /// </summary>
    public class EssentialController : ControllerBase
    {
        /// <summary>
        /// 程序执行过程中遇到逻辑错误时返回
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [NonAction]
        public ActionResult BusinesssError([ActionResultObjectValue] object value)
        {
            return StatusCode(GlobalConfig.BUSINESS_ERROR_HTTP_CODE, value);
        }

        /// <summary>
        /// 当前用户 Id
        /// </summary>
        public string CurrentUserId
        {
            get
            {
                return HttpContext.User.Identity.Name;
            }
        }

        /// <summary>
        /// 当前用户角色
        /// </summary>
        public List<string> CurrentUserRoleCodes
        {
            get
            {
                return HttpContext.User.Claims.Where(a => a.Type == ClaimTypes.Role)
                                                .Select(a => a.Value).ToList();
            }
        }

        /// <summary>
        /// 基础的上传文件方法
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        [NonAction]
        public ActionResult EssentialUploadFile(string dir)
        {
            ExecResult<string> execResult = CommonUtils.SaveUploadFile(Request.Form.Files, dir);
            if (execResult.Success)
            {
                return Ok(new Result
                {
                    Success = true,
                    Data = CommonUtils.ConvertLocalFSPathToURLPart(execResult.Data),
                    Msg = execResult.ErrorMsg
                });
            }
            else
            {
                return this.BusinesssError(new Result
                {
                    Success = false,
                    Msg = execResult.ErrorMsg
                });
            }
        }

        /// <summary>
        /// 基础的上传文件方法（多个）
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        [NonAction]
        public ActionResult EssentialUploadFiles(string dir)
        {
            ExecResult<List<string>> execResult = CommonUtils.SaveUploadFiles(Request.Form.Files, dir);
            execResult.Data = execResult.Data.Select(a =>
                                CommonUtils.ConvertLocalFSPathToURLPart(a)).ToList();
            if (execResult.Success)
            {
                return Ok(new Result
                {
                    Success = true,
                    Data = execResult.Data,
                    Msg = execResult.ErrorMsg
                });
            }
            else
            {
                return this.BusinesssError(new Result
                {
                    Success = false,
                    Msg = execResult.ErrorMsg
                });
            }
        }
    }
}
